package com.nycreativity.elecreativity.service.impl;

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.nycreativity.elecreativity.entity.enums.IsDelEnum
import com.nycreativity.elecreativity.entity.param.brand.BrandAddParam
import com.nycreativity.elecreativity.entity.param.brand.BrandUpdateParam
import com.nycreativity.elecreativity.entity.po.Brand
import com.nycreativity.elecreativity.entity.po.Goods
import com.nycreativity.elecreativity.entity.vo.brand.BrandVO
import com.nycreativity.elecreativity.mapper.BrandMapper
import com.nycreativity.elecreativity.mapper.GoodsMapper
import com.nycreativity.elecreativity.mapper.vomapper.BrandVOMapper
import com.nycreativity.elecreativity.service.BrandService
import com.nycreativity.elecreativity.util.MappingUtil
import com.nycreativity.elecreativity.util.usersession.UserContext
import org.springframework.stereotype.Service
import java.time.LocalDateTime
import javax.annotation.Resource

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-03-11
 */
@Service
open class BrandServiceImpl : ServiceImpl<BrandMapper, Brand>(), BrandService {

    @Resource
    lateinit var brandMapper: BrandMapper

    @Resource
    lateinit var brandVOMapper: BrandVOMapper

    @Resource
    lateinit var goodsMapper: GoodsMapper

    override fun queryById(id: Long): BrandVO {
        return brandVOMapper.selectById(id)
    }

    override fun add(brandAddParam: BrandAddParam): Boolean {
        val brand = MappingUtil.getMappingObject(brandAddParam, Brand::class.java)
        if (brand == null || brand.isEmpty()) {
            return false
        }
        brand.creator = UserContext.get()?.id
        brand.createTime = LocalDateTime.now()
        brandMapper.insert(brand)
        return true
    }

    override fun updateById(brandUpdateParam: BrandUpdateParam): Boolean {
        val brand = MappingUtil.getMappingObject(brandUpdateParam, Brand::class.java)
        if (brand == null || brand.isEmpty()) {
            return false
        }
        brand.modifier = UserContext.get()?.id
        brand.modifyTime = LocalDateTime.now()
        brandMapper.updateById(brand)
        return true
    }

    override fun queryAll(): List<BrandVO> {
        return brandVOMapper.selectList(null);
    }

    override fun deleteById(id: Long): Boolean {

        //判断是否存在
        val brand = brandMapper.selectById(id) ?: return true

        //判断是否有goods还是该brand
        val queryWrapper = KtQueryWrapper(Goods::class.java).eq(Goods::brandId, id)
        val list = goodsMapper.selectList(queryWrapper)
        list?.let { if (it.isNotEmpty()) return false }

        //删除
        val updateWrapper = KtUpdateWrapper(Brand::class.java)
            .set(Brand::isDel, IsDelEnum.YES.code)
            .set(Brand::modifier, UserContext.get()?.id)
            .set(Brand::modifyTime, LocalDateTime.now())
            .eq(Brand::id, id)
        brandMapper.update(null, updateWrapper)

        return true
    }

}
